﻿using WorkflowCore.Interface;
using WorkflowCore2210A.Application;
using WorkflowCore2210A.Domain;
using WorkflowCore2210A.Domain.Model;

namespace WorkflowCore2210A.Host
{
    public static class WorkflowExssion
    {
        public static void UseWorkflow(this WebApplication app)
        {

            var host = app.Services.GetService<IWorkflowHost>();

            app.Lifetime.ApplicationStarted.Register(() =>
            {
                host.Start();
                // 服务提供商
                IServiceProvider scope = app.Services.CreateScope().ServiceProvider;
                var definitionRepository = scope.GetService<IBaseRepository<WorkflowDefinitionModel>>();
                var workflowService = scope.GetService<IWorkflowService>();
                var list = definitionRepository.GetAll().Where(p => !p.IsDeleted);

                foreach (var definition in list)
                {
                    workflowService.LoadDefinition(definition);
                }
            });


            app.Lifetime.ApplicationStopped.Register(() =>
            {
                host.Stop();
            });

        }
    }
}
